import Axios from 'axios'
import store from '../index'

const user = {
  state: {
    name: '',
    avatar: '',
    roles: [],
    access_token: get('access_token'),
    authen: get('authen', { json: true }),
    authen_select_item: get('authen_select_item', { json: true }),
    user_info: get('user_info', { json: true }),
    btnStatus: ''
  },

  mutations: {
    CHANGE_STATUS: state => {
      state.btnStatus = '0'
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    authen: (state, authen) => {
      state.authen = authen
      window.localStorage.setItem('authen', JSON.stringify(authen))
    },
    access_token(state, access_token) {
      state.access_token = access_token
      window.localStorage.setItem('access_token', access_token)
    },
    authen_select_item: (state, authen_select_item) => {
      state.authen_select_item = authen_select_item
      window.localStorage.setItem('authen_select_item', JSON.stringify(authen_select_item))
    },
    getInfo(state, payload) {
      Axios.post(process.env.SERVER_URL_4A + '/account', {
        'access_token': payload.access_token,
        'type': 'get_account_base'
      }).then((res) => {
        console.log(res)
        save(state, 'user_info', res.data.data, { json: true })
      }).catch(function(error) {
        console.log(error)
      })

      Axios.post(process.env.SERVER_URL_4A + '/authen', {
        'access_token': payload.access_token,
        'type': 'get_rts_menu'
      }, {
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        isJSON: true
      }).then((res) => {
        store.commit('authen', res.data.datas)
        if (res.data.datas.length > 0) {
          store.commit('authen_select_item', res.data.datas[0])
          setTimeout(function() {
            window.location.href = payload.url
            console.log('11111', 11111)
          }, 1000)
        }
      }).catch(function(error) {
        console.log(error)
      })
    }
  },

  actions: {
    // 管理安全设置状态
    changeStatus: ({ commit }) => {
      commit('CHANGE_STATUS')
    },
    // // 登录
    // Login({ commit }, userInfo) {
    //   const username = userInfo.username.trim()
    //   return new Promise((resolve, reject) => {
    //     login(username, userInfo.password).then(response => {
    //       const data = response.data
    //       setToken(data.token)
    //       commit('SET_TOKEN', data.token)
    //       resolve()
    //     }).catch(error => {
    //       reject(error)
    //     })
    //   })
    // },
    login({ commit, state }, payload) {
      state.access_token = payload.access_token
      window.localStorage.setItem('access_token', payload.access_token)
      commit('getInfo', payload)
    },
    // 登出
    LogOut({ commit, state }) {
      Axios.post(process.env.SERVER_URL_4A + '/logout'
        , {
          access_token: store.getters.access_token
        }, {
          isJSON: true
        }).then((response) => {
        commit('authen', [])
        commit('access_token', '')
        commit('authen_select_item')
        console.log('process.env.NODE_ENV', process.env.SERVER_URL_4A_VUE)
        // window.location.href = process.env.SERVER_URL_4A_VUE + '?callbackUrl=' + encodeURIComponent(window.location.href)
        window.location.href = process.env.SERVER_URL_4A_VUE + '?callbackUrl=' + encodeURIComponent('http://' + window.location.host + '/#/')
      }).catch((error) => {
        commit('authen', [])
        commit('access_token', '')
        commit('authen_select_item')
        window.location.href = process.env.SERVER_URL_4A_VUE + '?callbackUrl=' + encodeURIComponent(window.location.href)
        console.log(error)
      })
    }
    //
    // // 前端 登出
    // FedLogOut({ commit }) {
    //   return new Promise(resolve => {
    //     commit('SET_TOKEN', '')
    //     removeToken()
    //     resolve()
    //   })
    // }
  }
}

function get(key, po) {
  const item = window.localStorage.getItem(key)
  if (item && item !== 'undefined') {
    if (po) {
      if (po.json) {
        return JSON.parse(item)
      }
    }
  }
  return item
}

function save(state, key, data, po) {
  state[key] = data
  if (po) {
    if (po.json) {
      window.localStorage.setItem(key, JSON.stringify(data))
    } else {
      window.localStorage.setItem(key, data)
    }
  }
}

export default user
